<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\User;
use App\Models\CarPackageAttrPermission;
use App\Models\Department;
use App\Models\Store;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class StaffController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(User::with(['store', 'department']), function (Grid $grid) {
            $grid->model()->where('type', MEMBER_TYPE_STAFF);
            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('avatar')->gallery();
            $grid->column('phone');
            $grid->column('balance');
            $grid->column('id_card');
            $grid->column('type')->using(admin_trans('staff.options.type'));
            $grid->column('store.title', admin_trans('staff.fields.store_id'));
            $grid->column('department.title', admin_trans('staff.fields.department_id'));
            $grid->column('updated_at')->sortable();
            $grid->model()->orderBy('id', 'desc');
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->like('name');
                $filter->like('phone');
            });

            $grid->disableDeleteButton();
            $grid->disableCreateButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, User::with(['store', 'department']), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('avatar')->image();
            $show->field('phone');
            $show->field('balance');
            $show->field('id_card');
            $show->field('type')->using(admin_trans('staff.options.type'));
            $show->field('store.title',  admin_trans('staff.fields.store_id'));
            $show->field('department.title', admin_trans('staff.fields.department_id'));
            $show->field('created_at');
            $show->field('updated_at');
            $show->panel()->tools(function ($tools) {
                $tools->disableDelete();
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new User(), function (Form $form) {
            $form->display('id');
            $form->text('name')->required();
            $form->image('avatar')->required();
            $form->text('phone')->required();
            $form->text('balance');
            $form->text('id_card');
            $form->radio('type', admin_trans('user.fields.type'))
                ->when(1, function (Form $form) {})
                ->when([2], function (Form $form) {
                    $package_attr_permission_ids = CarPackageAttrPermission::where('status', DEFAULT_STATUS_SUCCESS)->pluck('title', 'id');
                    $form->multipleSelect('package_attr_permission_ids')->options($package_attr_permission_ids)->saveAsJson();
                })
                ->options(admin_trans('user.options.type'))
                ->default(1);
            $form->select('store_id')
                ->options(Store::pluck('title', 'id'))
                ->load('department_id', 'staff-department');
            $form->select('department_id');
            $form->display('created_at');
            $form->display('updated_at');

            $form->tools(function (Form\Tools $tools) {
                $tools->disableDelete();
            });
        });
    }

    public function staffDepartment(Request $request)
    {
        $store_id = $request->get('q');

        return Department::where('store_id', $store_id)
            ->get(['id', DB::raw('title as text')]);
    }
}
